package code.juc.stopThread;

/**
 * 第一种停止线程的方法： 线程中没有阻塞情况时.
 *
 * 需要线程内部, 增加isInterrupted()方法的调用; 这样才能响应其它线程的interrupt()方法的调用.
 */
public class RightWayStopThreadWithoutSleep implements Runnable {
    @Override
    public void run() {
        int num = 0;
        while (!Thread.currentThread().isInterrupted() && num <= Integer.MAX_VALUE / 2) {
            if (num % 10000 == 0) {
                System.out.println("This num is " + num + " mod 10000");
            }

            num++;
        }

        System.out.println("Thread is finished!");
    }

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new RightWayStopThreadWithoutSleep());
        thread.start();
        Thread.sleep(1000);
        thread.interrupt();
    }
}
